﻿

using SmartHRMS.Model.Account;
using StructureMap;
using System.Collections.Generic;
using System;

namespace SmartHRMS.Presentation.Account
{
   public class RoleListPresenter
    {
        private IRoleListView view;
        private IRoleService roleService;

        public RoleListPresenter(IRoleListView view)
            : this(ObjectFactory.GetInstance<IRoleService>(), view)
        {
        }

        public RoleListPresenter(IRoleService roleService, IRoleListView view)
        {
            this.view = view;
            this.roleService = roleService;
        }

        public void Display()
        {
            clearInfoAndErrorMessages();

            IList<Role> allRoles;

            allRoles = roleService.FindAll();

            this.view.Role = allRoles;
        }

        public void RemoveRoleBy(Guid roleId)
        {
            clearInfoAndErrorMessages();

            Role role;
            role = roleService.FindBy(roleId);

            if (role != null)
            {
                roleService.Remove(role);
                Display();
            }
            else
            {
                // Update error view
                this.view.ErrorMessage(String.Format("A role with an Id of '{}' cannot be found", roleId));
            }
        }

        private void clearInfoAndErrorMessages()
        {
            view.ErrorMessage(string.Empty);
            view.InfoMessage(string.Empty);
        }
    }
}

